package l2hc.gameserver.skills.skillclasses;

import l2hc.commons.collections.GArray;

import l2hc.gameserver.Config;
import l2hc.gameserver.model.Creature;
import l2hc.gameserver.model.Player;
import l2hc.gameserver.model.Skill;
import l2hc.gameserver.templates.StatsSet;

public class SelfSacrifice extends Skill
{
  private final int _effRadius;

  public SelfSacrifice(StatsSet set)
  {
    super(set);
    this._effRadius = set.getInteger("effRadius", 1000);
    this._lethal1 = set.getInteger("lethal1", 0);
    this._lethal2 = set.getInteger("lethal2", 0);
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
public GArray<Creature> getTargets(Creature activeChar, Creature aimingTarget, boolean forceUse)
  {
	  	GArray result = new GArray();
	    GArray<Creature> targets = activeChar.getAroundCharacters(this._effRadius, 1000);
	    if (((targets == null) || (targets.isEmpty())) && (((Player)activeChar).getParty() == null)) {
	      return result;
	    }
	    for (Creature target : targets)
	    {
	      if ((target == null) || 
	        (!target.isPlayer()) || 
	        (target.isAutoAttackable(activeChar))) {
	        continue;
	      }
      if (target.isPlayer())
      {
        Player activeCharTarget = (Player)target;
        Player activeCharPlayer = (Player)activeChar;
        if ((activeCharTarget.isInDuel()) || 
          (activeCharTarget.isCursedWeaponEquipped())) {
          continue;
        }
        boolean _party = activeCharTarget.isInParty();
        boolean _partySelf = activeCharPlayer.isInParty();
        boolean _isInSameParty = false;

        boolean _clan = activeCharTarget.getClan() != null;
        boolean _clanSelf = activeCharPlayer.getClan() != null;
        boolean _isInSameClan = false;

        boolean _ally = activeCharTarget.getAlliance() != null;
        boolean _allySelf = activeCharPlayer.getAlliance() != null;
        boolean isInSameAlly = false;

        if ((_party) && (_partySelf)) {
          for (Player member : activeCharPlayer.getParty().getPartyMembers())
            if (member == activeCharTarget)
             _isInSameParty = true;
        }
        if ((_clan) && (_clanSelf)) {
          for (Player clanMember : activeCharPlayer.getClan().getOnlineMembers(0))
            if (clanMember == activeCharTarget)
             _isInSameClan = true;
        }
        if ((_ally) && (_allySelf) && 
         (activeCharPlayer.getClan().getAllyId() == activeCharTarget.getClan().getAllyId()) && (activeCharPlayer.getClan().getAllyId() != 0)) {
          isInSameAlly = true;
        }
        if ((!isInSameAlly) && (!_isInSameClan) && (!_isInSameParty)) {
          continue;
        }
      }
      result.add(target);
    }
    return result;
  }

  public void useSkill(Creature activeChar, GArray<Creature> targets)
  {
    for (Creature target : targets) {
      if (target != null)
      {
        if ((getSkillType() == Skill.SkillType.BUFF) && (target != activeChar) && (
          (target.isCursedWeaponEquipped()) || (activeChar.isCursedWeaponEquipped()))) {
          continue;
        }
        boolean reflected = target.checkReflectSkill(activeChar, this);
        getEffects(activeChar, target, getActivateRate() > 0, false, reflected);
      }
    }
    if ((isSSPossible()) && (
      (!Config.SAVING_SPS) || (this._skillType != Skill.SkillType.SELF_SACRIFICE)))
      activeChar.unChargeShots(isMagic());
  }
}